//
//  ZDVisterView.swift
//  ZZDWeibo
//
//  Created by zzdan on 16/6/25.
//  Copyright © 2016年 zzdan. All rights reserved.
//

import UIKit
import SnapKit

class ZDVisterView: UIView {
    
    //创建闭包
    var closure:(()->())?
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  懒加载控件
    
    //圆圈
    private lazy var feedImagView: UIImageView =
    UIImageView(imgName: "visitordiscover_feed_image_smallicon")
    
    //遮罩
    private lazy var maskImageView:UIImageView = UIImageView(imgName: "visitordiscover_feed_mask_smallicon")
    
    
    //房子
    private lazy var iconImageView:UIImageView = UIImageView(imgName: "visitordiscover_feed_image_house")
    
    //文字
    private lazy var messageLabel:UILabel = {
        
        let lab = UILabel()
        
        lab.textColor = ThemeColor
        lab.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        lab.font = UIFont.systemFontOfSize(14)
        lab.textAlignment = .Center
        lab.numberOfLines = 0
        
        return lab
        
    }()
    
    
    //登录按钮
    lazy var loginBtn: UIButton = {
    
    let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
       }()
    
    //注册按钮
    lazy var registerBtn: UIButton = {
        
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    
    // MARK: -  按钮的点击事件
    @objc private func btnClick(){
        
        //执行闭包
        closure?()
        
    }
    
    // MARK: -  供外界调用的方法(每个控制器的区别)
    func setupVistorViewInfo(imageName:String?,title:String?){
        
        if let image = imageName,let title = title {
            
            iconImageView.image = UIImage(named: image)
            messageLabel.text = title
            feedImagView.hidden = true
            
        }else{
            
            //是主页
            setupAnim()
            
        }
        
        
    }
    
    
    // MARK: -  设置圆圈的动画
    private func setupAnim(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.duration = 10
        anim.toValue = M_PI * 2
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        
        //添加动画
        feedImagView.layer.addAnimation(anim, forKey: nil)
    }
    

    
    
    // MARK: -  设置视图
    private func setupUI(){
        
        backgroundColor = RGB(237, green: 237, blue: 237)
        
        
        
        //添加控件
        addSubview(feedImagView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        //添加约束
        
        //圆圈
        feedImagView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
        //遮罩
        maskImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
       //房子
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self)
        }
        
        //文字
        messageLabel.snp_makeConstraints { (make) -> Void in
            
            make.width.equalTo(224)
            make.height.equalTo(36)
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(maskImageView)

        }
        
        //登录
        loginBtn.snp_makeConstraints { (make) -> Void in
            
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
        }
        
        //注册
        registerBtn.snp_makeConstraints { (make) -> Void in
            
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(loginBtn)
        }
        
        
        
    }

}


